//
//  TopicListViewController.m
//  Leisure
//
//  Created by xalo on 16/7/1.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TopicListViewController.h"
#import <Foundation/Foundation.h>
@interface TopicListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)UIImageView *backgroundImageView;
@property (nonatomic,strong)UIView *views;
@property (nonatomic,strong)UIView *viewss;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *contentLabel;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,assign)NSInteger newestStart;
@property (nonatomic,strong)NSString *commentString;

@end

@implementation TopicListViewController


-(UIImageView *)backgroundImageView{
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 200, kWidth-20, 200)];
    }
    
    return _backgroundImageView;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, kWidth-20,80)];
        _titleLabel.text = self.model.title;
        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:21];
    }
    return _titleLabel;
}
-(UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.titleLabel.frame), CGRectGetMaxY(self.titleLabel.frame)+20, kWidth-20, 300)] ;
        _contentLabel.text = self.model.content;
        _contentLabel.font = [UIFont systemFontOfSize:16];

    }
    return _contentLabel;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicCommentCell" bundle:nil] forCellReuseIdentifier:@"topicCommentCell"];
}
//创建TableHeaderView
-(void)createHeaderView{
    
    if (self.model.coverimg.length != 0) {

        //背景图自适应
        [self.backgroundImageView setFrame:CGRectMake(self.contentLabel.frame.origin.x, [self.contentLabel sizeThatFits:CGSizeMake(kWidth-20, CGFLOAT_MAX)].height+self.contentLabel.frame.origin.y+10, kWidth-20, [SizeToFitTool fixHeightWithImageView:self.backgroundImageView imageWidth:kWidth -20])] ;

        self.views = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, self.backgroundImageView.frame.size.height + self.backgroundImageView.frame.origin.y)];
        [self.tableView setTableHeaderView:self.views];

    }else if(self.model.coverimg.length == 0)  {
        self.viewss = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, self.contentLabel.frame.size.height+self.contentLabel.frame.origin.y)];
//        self.viewss.backgroundColor = [UIColor redColor];
        [self.tableView setTableHeaderView:self.viewss];
    }

//    [self.tableView.tableHeaderView sizeToFit];
    
    [self loadHotViewWithStart:0 limit:20];
    
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.dataSource removeAllObjects];
        [self loadHotViewWithStart:0 limit:20];
    }];
    self.tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newestStart += 10;
        [self loadHotViewWithStart:self.newestStart limit:10];
    }];
}

-(void)loadHotViewWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kTopicListURL parameter:@{@"sort":@"hot",@"contentid":self.contentID,@"start":@(start),@"limit":@(limit)} method:@"POST" completed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dict in dic[@"data"][@"commentlist"]) {
            TopicListModel *model = [[TopicListModel  alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:self.model.coverimg]];
    
    self.titleLabel.numberOfLines = 0;
    self.contentLabel.numberOfLines = 0;

    NSLog(@"---%@",self.contentLabel.text);
    //设置首行缩进
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle defaultParagraphStyle].mutableCopy;
    paragraph.firstLineHeadIndent = 40.f;
    //设置行间距
//    paragraph.lineSpacing = 5;
//    paragraph.headIndent = 10.f;//行左缩进
    NSDictionary *attribute = @{NSParagraphStyleAttributeName:paragraph};
    NSAttributedString *attributeString = [[NSAttributedString alloc]initWithString:self.contentLabel.text attributes:attribute];
    self.contentLabel.attributedText = attributeString;
    //标题自适应
    [self.titleLabel setFrame:CGRectMake(10, 0, kWidth-20,[self.titleLabel sizeThatFits:CGSizeMake(kWidth-20, CGFLOAT_MAX)].height)];
    //内容自适应
    [self.contentLabel setFrame:CGRectMake(10, self.titleLabel.frame.size.height+10, kWidth-20, [self.contentLabel sizeThatFits:CGSizeMake(kWidth-20, CGFLOAT_MAX)].height)];
    [self createHeaderView];

    [self.views addSubview:self.titleLabel];
    [self.views addSubview:self.contentLabel];
    [self.views addSubview:self.backgroundImageView];
    
    [self.viewss addSubview:self.titleLabel];
    [self.viewss addSubview:self.contentLabel];
//    self.views.backgroundColor = [UIColor greenColor];
//    self.contentLabel.backgroundColor = [UIColor blueColor];
//    self.titleLabel.backgroundColor = [UIColor purpleColor];
}
#pragma mark 绘制cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicCommentCell" forIndexPath:indexPath];
    cell.iconImage.layer.cornerRadius = cell.iconImage.frame.size.width/2;
    cell.iconImage.layer.masksToBounds = YES;
    [cell setCellWithModel:self.dataSource[indexPath.row]];
//    [cell setFrame:CGRectMake(0, 0, kWidth, [cell returnHeightOfCell])];
    self.commentString = cell.content.text;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SizeToFitTool fixHeightToLabelText:self.commentString font:[UIFont systemFontOfSize:17] label:kWidth -20]+120;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end